<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="900px">
    <el-form ref="goodsform" :model="goodform" :rules="rules" :inline="true" label-width="80px" size="small" >
      <el-form-item label="产品名称" label-width="100px">
        <el-select v-model="goodform.goodname" placeholder="产品名称" size="mini" class="filter-item" style="width:150px">
          <el-option v-for="item in dicts" :key="item.id" :label="item.value" :value="item.value">{{ item.label }}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数量" label-width="100px">
        <el-input-number v-model="goodform.goodcount" size="small"/>
      </el-form-item>
      <el-form-item label="价格" label-width="100px">
        <el-input-number v-model="goodform.goodprice" size="small"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit, findGoodsByid } from '@/api/goods'
import initDict from '@/mixins/initDict'
export default {
  mixins: [initDict],
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      loading: false, dialog: false,
      row: null,
      goodform: {
        id: '',
        bushutongjiid: '',
        goodname: '',
        goodcount: '',
        goodprice: ''
      }, options: [
      ],
      rules: {
      }
    }
  },
  created() {
    this.$nextTick(() => {
      // 加载数据字典
      this.getDict('goods_name')
    })
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.loading = true
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      add(this.goodform).then(res => {
        this.resetForm()
        this.$notify({
          title: '新增成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        console.log(this.row)
        findGoodsByid(this.row.id).then(res => {
          this.$parent.goods = res
        })
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      edit(this.goodform).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        // his.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['goodsform'].resetFields()
      this.goodform = {
        id: '',
        bushutongjiid: '',
        goodname: '',
        goodcount: '',
        goodpirce: ''
      }
    }
  }
}
</script>

<style scoped>

</style>
